//
//  SWPresentationManager.swift
//  SwiftWB
//
//  Created by 钟清 on 17/3/28.
//  Copyright © 2017年 Zhong Qing. All rights reserved.
//

import UIKit

/// 自定转场展现
let SWPresentationManagerDidPresented = "SWPresentationManagerDidPresented"
/// 自定义转场消失
let SWPresentationManagerDidDismissed = "SWPresentationManagerDidDismissed"

class SWPresentationManager: NSObject,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning {
   
    /// 标记：记录当前视图是展现
    private var isPresent = false
    /// 尺寸
    var presentFrame = CGRectZero
    
    // MARK: - UIViewControllerTransitioningDelegate
    /// 控制尺寸
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        let pop = SWPresentationController(presentedViewController: presented, presentingViewController: presenting)
        pop.presentFrame = presentFrame
        return pop
    }
    /// 控制消失
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        NSNotificationCenter.defaultCenter().postNotificationName(SWPresentationManagerDidDismissed, object: self)
        return self
    }
    /// 控制展现
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        NSNotificationCenter.defaultCenter().postNotificationName(SWPresentationManagerDidPresented, object: self)
        return self
    }
    
    // MARK: - UIViewControllerAnimatedTransitioning
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        if isPresent { // 视图展现
            
            willPresentController(transitionContext)
            
        } else { // 视图消失
            willDismissController(transitionContext)
    }
}
    
    /// 执行展现动画
    private func willPresentController(transitionContext: UIViewControllerContextTransitioning) {
        guard let toView = transitionContext.viewForKey(UITransitionContextToViewKey) else {
            return
        }
        transitionContext.containerView()?.addSubview(toView)
        toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        toView.transform = CGAffineTransformMakeScale(1.0, 0.0)
        UIView.animateWithDuration(transitionDuration(transitionContext)) {
            toView.transform = CGAffineTransformIdentity
            transitionContext.completeTransition(true)
    }
}
    
    /// 执行消失动画
    private func willDismissController(transitionContext: UIViewControllerContextTransitioning) {
            guard let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey) else {
                return
            }
            fromView.transform = CGAffineTransformMakeScale(1.0, 0.0001)
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
                transitionContext.completeTransition(true)
            })
        }
}

